<?php
/**
 * Created by PhpStorm.
 * User: yaoihu
 * Date: 2019/6/14
 * Time: 23:56
 */

namespace DC\BonusCenter\Services;


use App\Models\PlatFinanceModel;
use App\Models\Rule\BonusRuleModel;
use App\Models\Shop\UserAchievementModel;
use App\Models\User\AccountFlowModel;
use Carbon\Carbon;
use DC\BonusCenter\CommonBonus;
use DC\UserCenter\Models\UserAuthModel;
use DC\UserCenter\Models\UserBaseModel;
use App\Models\Bonus\BonusCountModel;
use DC\UserCenter\Models\UserInfoModel;
use DC\UserCenter\Models\UserWalletModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ShoppingBonus extends CommonBonus
{

    public function award($new_uid,$money ,$zone,$area_address,$dan=0)
    {
        if ($money <= 0) {
            return false;
        }
        $info = UserBaseModel::where('member_id',$new_uid)->where('status','>=',0)->first();
        if(!$info){
            return false;
        }
        if($zone==1){//报单区商品，默认只能买一件
            $bs_fensi=BonusRuleModel::where('name','fensi3')->first();
            if($money!=$bs_fensi->rule1){
                return false;
            }
            if($info->fs_level==0){
                $upfs_level=$bs_fensi->level;
                $info->fs_level=$upfs_level;
                $info->save();
                //增加业绩
                $this->addteam($info);
                //升级车队级别
                $this->uplevel_cd($info);
            }


        }elseif($zone==2){//成长值专区
            //升级 发奖
            $uplevel=$info->level+1;
            if(!in_array($uplevel,[1,2,3,4,5,6,7,8,9,10,11])){
              return false;
            }
            $bc_reg=BonusRuleModel::where('type','reg')->where('level',$uplevel)->first();
            if($money!=$bc_reg->rule1){
                return false;
            }
            //升级
            $info->level=$uplevel;
            $info->save();
            //发奖
            $jdids = array_reverse(str2arr($info->jiedian_ids));
            $jdeep=$info->jdeep-$uplevel;
            $hj_info=UserBaseModel::where('status','>=',0)->whereIn('member_id', $jdids)->where('level','>=',$uplevel)->where('jdeep',$jdeep)->first();
            if(!$hj_info){
                return false;
            }
            $bc_fee=BonusRuleModel::where('name','fee')->first();
            $fee=$bc_fee->rule1;
            $allot = [
                ['field'=>'money2','rates'=>100-$fee,'type'=>1],
                ['field'=>'plat_fee','rates'=>$fee,'type'=>0]
            ];

            $hj_money=$money;
            if($hj_money>0){
                $this->setBonusAllot($allot)->settleBonus(42,$hj_info->member_id,$info->member_id,$hj_money,2);
            }
        }

        return true ;
    }
    /**
     *  增加有效直推和有效团队
     */
    public function addteam($info){
        $num = 1;
        //有效团队
        $tddata['num_td_yx'] = DB::raw('`num_td_yx`+' . $num);
        UserBaseModel::where('status','>=',0) ->whereRaw("FIND_IN_SET(`member_id`,?)", [$info->referrer_ids])->update($tddata);
        //有效直推
        $referrer_data['num_zt_yx'] = DB::raw('`num_zt_yx`+' . $num);
        UserBaseModel::where('status','>=',0)->where('member_id', $info->referrer_id)->update($referrer_data);
        return true ;
    }

    /**
     * 升级车队级别
     * @param $info
     */
    public function uplevel_cd($info){
        $bc_youbu=BonusRuleModel::where('type','youbu')->get()->toArray();
        $bc_youbu_arr=[];
        foreach($bc_youbu as $byv){
            $bc_youbu_arr[$byv['level']]=$byv;
        }
        $tjids = array_reverse(str2arr($info->referrer_ids));
        $tj_list=UserBaseModel::where('status','>=',0)->whereIn('member_id', $tjids)->get();
        foreach($tj_list as $tlv){

            $up_cd_level=$tlv->cd_level+1;
            if(!in_array($up_cd_level,[1,2,3,4,5,6,7,8,9,10,11])){
                continue;
            }
            Log::info($tlv->member_id,$bc_youbu_arr[$up_cd_level]);
            //判断有效直推
            if($bc_youbu_arr[$up_cd_level]['rule3']>0){
                if($tlv->num_zt_yx<$bc_youbu_arr[$up_cd_level]['rule3']){
                    continue;
                }
            }
            //判断大车队有效团队
            if($bc_youbu_arr[$up_cd_level]['rule4']>0){
                $max_td_yx=UserBaseModel::where('referrer_id',$tlv->member_id)->max('num_td_yx');
                Log::info($tlv->member_id.'max_td_yx',['max_td_yx'=>$max_td_yx]);
                if($max_td_yx<$bc_youbu_arr[$up_cd_level]['rule4']){
                    continue;
                }
            }
            //判断其余车队有效团队
            if($bc_youbu_arr[$up_cd_level]['rule5']>0){
                $sum_td_yx=UserBaseModel::where('referrer_id',$tlv->member_id)->sum('num_td_yx');
                Log::info($tlv->member_id.'sum_td_yx',['sum_td_yx'=>$sum_td_yx]);

                $other_td_yx=$sum_td_yx-$max_td_yx;
                if($other_td_yx<$bc_youbu_arr[$up_cd_level]['rule5']){
                    continue;
                }
            }
            //判断自己大有效团队
            if($bc_youbu_arr[$up_cd_level]['rule6']>0){
                if($tlv->num_td_yx<$bc_youbu_arr[$up_cd_level]['rule5']){
                    continue;
                }
            }
            //可以升级
            $tlv->cd_level=$up_cd_level;
            $tlv->gz_day_can=$bc_youbu_arr[$up_cd_level]['rule2'];
            $tlv->gz_day_val=$bc_youbu_arr[$up_cd_level]['rule1'];
            $tlv->gz_day_had=0;
            $tlv->save();
        }
    }



    /**
     * 工资结算-for2037油补
     */
    public function gongzi(){
        $bc_fee=BonusRuleModel::where('name','fee')->first();
        $fee=$bc_fee->rule1;
        $allot = [
            ['field'=>'money3','rates'=>100-$fee,'type'=>1],
            ['field'=>'plat_fee','rates'=>$fee,'type'=>0]
        ];

        $user_list=UserBaseModel::where('status','>',0)->where('gz_day_val','>',0)->where('gz_day_can','>',0)->get();
        foreach ($user_list as $ulv){
            if($ulv->gz_day_had>=$ulv->gz_day_can){
                continue;
            }
            $hj_money=$ulv->gz_day_val;
            if($hj_money>0){
                $this->setBonusAllot($allot)->settleBonus(43,$ulv->member_id,$ulv->member_id,$hj_money,3);
                //增加天数判断滑落
                $ulv->gz_day_had=$ulv->gz_day_had+1;
                $ulv->save();
            }
        }
    }


    /**
     * for2037end
     * *************************************************
     */





}